//
//  NetService.m
//  TheRicher
//
//  Created by 李晓东 on 2018/1/17.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "NetService.h"

@interface NetService ()

@property (nonatomic, strong) NSString *appcode;
@property (nonatomic, strong) NSString *host;
@property (nonatomic, strong) NSString *path;
@property (nonatomic, strong) NSString *method;
@property (nonatomic, strong) NSString *querys;

@property (nonatomic, strong) NetQueryResult netQueryResultBlock;

@end

@implementation NetService

+ (id)share{
    static NetService *netService = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        netService = [[NetService alloc]init];
    });
    return netService;
}

- (instancetype)init{
    if (self = [super init]) {
        _appcode = @"5ca6cbf36a9d45a78a2fc4b071f2333e";
        _host = @"http://jisucpkj.market.alicloudapi.com";
        _method = @"GET";

    }
    return self;
}

- (void)loadDataFromIssueno:(NSString *)issueno andNumOf:(NSInteger)num andID:(NSInteger)id andCallBlock:(NetQueryResult)netQueryResultBlock{
    _path = @"/caipiao/history";
    _querys = [NSString stringWithFormat:@"?caipiaoid=%ld&issueno=%@&num=%ld", id, issueno, num];
    self.netQueryResultBlock = netQueryResultBlock;
    [self accessNetwork];
}

- (void)loadDataFromIssueno:(NSString *)issueno andID:(NSInteger)id andCallBlock:(NetQueryResult)netQueryResultBlock{
    _path = @"/caipiao/query";
    _querys = [NSString stringWithFormat:@"?caipiaoid=%ld&issueno=%@", id, issueno != nil ? issueno : @""];
    self.netQueryResultBlock = netQueryResultBlock;
    [self accessNetwork];
    
}

- (void)accessNetwork {
    NSString *url = [NSString stringWithFormat:@"%@%@%@",  _host,  _path , _querys];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url]  cachePolicy:1  timeoutInterval:  5];
    request.HTTPMethod  =  _method;
    [request addValue:  [NSString  stringWithFormat:@"APPCODE %@" ,  _appcode]  forHTTPHeaderField:  @"Authorization"];
    NSURLSession *requestSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionDataTask *task = [requestSession dataTaskWithRequest:request
                                                   completionHandler:^(NSData * _Nullable body , NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                                       id jsonObj = [NSJSONSerialization JSONObjectWithData:body options:NSJSONReadingAllowFragments error:nil];
                                                       
                                                       NSLog(@"Response object: %@" , response);
//                                                       self.netQueryResultBlock(jsonObj);
                                                       if ([self.delegate respondsToSelector:@selector(resultInNet:)]) {
                                                           [self.delegate resultInNet:jsonObj];
                                                       }
                                                       
                                                   }];
    [task resume];
}


@end


